from .exts import db


# 作者表
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20))
    age = db.Column(db.Integer, default=0)
    email = db.Column(db.String(120))
    sex = db.Column(db.Boolean, default=True)

    books = db.relationship('Book', backref='author', lazy='dynamic')
    # 关系建立在一对多的一
    # backref='author'意味着在Books模型中将自动创建一个名为author的属性，这个属性指向与书籍相关联的作者


# 书籍表
class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(200))
    date = db.Column(db.DateTime)
    author_id = db.Column(db.Integer, db.ForeignKey(Author.id))


# 书籍和出版社的关系表
book_publisher = db.Table('book_publisher',
                          db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
                          db.Column('publisher_id', db.Integer, db.ForeignKey('publisher.id')))


# 出版社表
class Publisher(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    publisher_name = db.Column(db.String(20))
    website = db.Column(db.String(100))
    city = db.Column(db.String(100))
    address = db.Column(db.String(100))
    province = db.Column(db.String(120))
    country = db.Column(db.String(120))
    books = db.relationship('Book', backref='publisher', secondary=book_publisher, lazy='dynamic')
